package com.fer.service.impl;

import com.fer.mapper.GoodsMapper;
import com.fer.mapper.OrdersMapper;
import com.fer.model.Goods;
import com.fer.model.Orders;
import com.fer.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * OrderService的实现类
 * 实现关于订单的功能
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    //添加订单的方法
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public boolean insertOrder(Orders orders,String[] goodsId) {
            List<Goods> goodsList = new ArrayList<>();
            float all_price = 0;
            //添加时间
            orders.setOrder_time(new Date());
            //设置完成状态(默认未完成)
            orders.setIs_finished(false);
            //添加总价
            for (String id : goodsId) {
                Goods goods = goodsMapper.selectGood(Integer.parseInt(id));
                if (goods.is_sale() != true) {
                    return false;
                }
                goodsList.add(goods);
                all_price += goods.getOut_price();
            }
//        System.out.println(goodsList);
//        System.out.println("===================================");
            //String a = null;
            orders.setAll_price(all_price);
            orders.setGoods_list(goodsList);
            //将order存入数据库并获取到order的id号
            ordersMapper.insertOrder(orders);
            System.out.println(orders);
            //System.out.println(a.length());
            //获取orders的id后存储商品信息
            for (Goods goods : goodsList) {
                ordersMapper.insertGoodsForOrder(orders.getOrder_id(), goods.getGoods_id());
//                System.out.println("成功");
            }
            return true;
    }

    //以下两个方法为查询订单的方法 此处为了用一个sql解决所有便有了单个查询时从list里单独拎出来的写法
    //查询单个
    @Override
    public Orders selectOneOrder(Integer order_id) {
        List<Orders> orders = ordersMapper.selectOrder(order_id);
        if (orders.size()==0){
            return null;
        }
        return orders.get(0);
    }
    //根据订单状态查询
    @Override
    public List<Orders> selectOrdersByState(boolean is_finished,Integer shop_id) {
        return ordersMapper.selectOrderByState(is_finished,shop_id);
    }
    //查询全部
    @Override
    public List<Orders> selectAllOrder(Integer shop_id) {
        return ordersMapper.selectOrderByShop(shop_id);
    }


    //这个就是修改的方法啦
    @Override
    public boolean updateOrder(Orders order) {
        int result = ordersMapper.updateOrder(order);
        if (result==-1){
            return false;
        }
        return true;
    }

    //呐呐,删除的方法啦
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void deleteOrder(Integer order_id) {
        ordersMapper.deleteGoodsForOrder(order_id);
        //System.out.println("相关商品已删除");
        ordersMapper.deleteOrder(order_id);
        //System.out.println("order表已删除");
    }


}
